<template><div><h2 id="重置密码" tabindex="-1"><a class="header-anchor" href="#重置密码"><span>重置密码</span></a></h2>
<p>Laravel 6 中文文档 /</p>
<p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="重置密码-1" tabindex="-1"><a class="header-anchor" href="#重置密码-1"><span>重置密码</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#resetting-database">数据库注意事项</a></li>
<li><a href="#resetting-routing">路由</a></li>
<li><a href="#resetting-views">视图</a></li>
<li><a href="#after-resetting-passwords">重置密码后</a></li>
<li><a href="#password-customization">自定义</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<blockquote>
<p>Tip：<strong>想要快速上手？</strong> 只需要在新建的 Laravel 应用中使用 Composer 安装 <code v-pre>laravel/ui</code> 拓展包并且运行 <code v-pre>php artisan ui vue --auth</code>， 然后在浏览器中打开 <code v-pre>http://your-app.test/register</code> 或者给你的应用分配任意一个 URL。 这个命令将会负责搭建起整个身份验证系统，包括重置密码！</p>
</blockquote>
<p>大多数 web 应用都为用户提供了重置密码的功能。相较于强迫你在每个应用中都要重新实现一遍此功能，Laravel 提供了便捷的方法来发送密码提醒以及执行密码重置。</p>
<blockquote>
<p>注意：在使用 Laravel 的密码重置功能之前，你的用户模型必须使用 <code v-pre>Illuminate\Notifications\Notifiable</code> trait。</p>
</blockquote>
<h2 id="数据库注意事项" tabindex="-1"><a class="header-anchor" href="#数据库注意事项"><span>数据库注意事项</span></a></h2>
<p>首先，验证你的 <code v-pre>App\User</code> 模型是否实现了 <code v-pre>Illuminate\Contracts\Auth\CanResetPassword</code> 契约。当然，框架中包含的 App\User 模型已经实现了该接口，并且使用了 <code v-pre>Illuminate\Auth\Passwords\CanResetPassword</code> trait来包含实现该接口所需的方法。</p>
<h4 id="生成重置令牌的表迁移" tabindex="-1"><a class="header-anchor" href="#生成重置令牌的表迁移"><span>生成重置令牌的表迁移</span></a></h4>
<p>接下来，必须创建一张数据表来存储密码重置令牌。该数据表的迁移已包含在 Laravel 应用的 <code v-pre>database/migrations</code> 目录中。 所以，你需要做的只是执行数据库迁移命令:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan migrate</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="路由" tabindex="-1"><a class="header-anchor" href="#路由"><span>路由</span></a></h2>
<p>Laravel 已在 <code v-pre>Auth\ForgotPasswordController</code> 和 <code v-pre>Auth\ResetPasswordController</code> 类中包含了发送密码重置链接电子邮件和重置用户密码的逻辑。 所有重置密码需要的路由都可以通过 Composer 的 <code v-pre>laravel/ui</code> 拓展包生成：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">composer <span class="token keyword">require</span> laravel<span class="token operator">/</span>ui</span>
<span class="line"></span>
<span class="line">php artisan ui vue <span class="token operator">--</span>auth</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="视图" tabindex="-1"><a class="header-anchor" href="#视图"><span>视图</span></a></h2>
<p>要生成重置密码必要的视图文件, 你可以使用 Composer 的<code v-pre>laravel/ui</code> 拓展包：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">composer <span class="token keyword">require</span> laravel<span class="token operator">/</span>ui</span>
<span class="line"></span>
<span class="line">php artisan ui vue <span class="token operator">--</span>auth</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>这些视图在 <code v-pre>resources/views/auth/passwords</code> 中。 你可以随意根据需要为您的应用程序自定义它们。</p>
<h2 id="重置密码后" tabindex="-1"><a class="header-anchor" href="#重置密码后"><span>重置密码后</span></a></h2>
<p>一旦你定义了重置用户密码的路由和视图，你可以在浏览器中访问 <code v-pre>/password/reset</code> 这个路由来重置密码。框架中的 <code v-pre>ForgotPasswordController</code> 已包含发送密码重置链接电子邮件的逻辑，同时 <code v-pre>ResetPasswordController</code> 包含了重置用户密码的逻辑。</p>
<p>在重置密码后，用户将会自动登录并重定向到 <code v-pre>/home</code>。 你可以通过在 <code v-pre>ResetPasswordController</code> 中定义一个 <code v-pre>redirectTo</code> 属性来自定义密码重置后重定向的位置:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">protected</span> <span class="token variable">$redirectTo</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'/dashboard'</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>注意：默认情况下，密码重置令牌会在一小时后过期。你可以通过 <code v-pre>config/auth.php</code> 文件中的密码重置 <code v-pre>expire</code> 选项对此进行修改。</p>
</blockquote>
<h2 id="自定义" tabindex="-1"><a class="header-anchor" href="#自定义"><span>自定义</span></a></h2>
<h4 id="自定义身份验证看守器" tabindex="-1"><a class="header-anchor" href="#自定义身份验证看守器"><span>自定义身份验证看守器</span></a></h4>
<p>在你的 <code v-pre>auth.php</code> 配置文件中，你可以配置多个「看守器」，可以用来定义多个用户表的身份验证行为。你可以自定义框架中的 <code v-pre>ResetPasswordController</code> ，通过重写该控制器中的 <code v-pre>guard</code> 方法来使用你所选择的看守器。这个方法应当返回一个看守器实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Auth</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 在密码重置期间看守</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token punctuation">\</span>Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>StatefulGuard</span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token keyword">function</span> <span class="token function-definition function">guard</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token class-name static-context">Auth</span><span class="token operator">::</span><span class="token function">guard</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'guard-name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="自定义密码代理" tabindex="-1"><a class="header-anchor" href="#自定义密码代理"><span>自定义密码代理</span></a></h4>
<p>在你的 <code v-pre>auth.php</code> 配置文件中，你可以配置多个密码「代理」，可以用来重置多个用户表上的密码。你可以自定义框架中的 <code v-pre>ForgotPasswordController</code> 和 <code v-pre>ResetPasswordController</code> ， 通过重写控制器中的 <code v-pre>broker</code> 方法来使用你所选择的代理：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Password</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取在密码重置期间使用的代理。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name">PasswordBroker</span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">broker</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">broker</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="自定义密码重置邮件" tabindex="-1"><a class="header-anchor" href="#自定义密码重置邮件"><span>自定义密码重置邮件</span></a></h4>
<p>你可以轻松地修改用于向用户发送密码重置链接的通知类。首先，重写 <code v-pre>User</code> 模型中的 <code v-pre>sendPasswordResetNotification</code> 方法。 在此方法中，你可以使用任何你所选择的通知类来发送通知。该方法接收的第一个参数是密码重置令牌 <code v-pre>$token</code> ：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 发送密码重置通知。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">string</span></span>  <span class="token parameter">$token</span></span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">sendPasswordResetNotification</span><span class="token punctuation">(</span><span class="token variable">$token</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">ResetPasswordNotification</span><span class="token punctuation">(</span><span class="token variable">$token</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/6.x/passwords/5157" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/pas...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/6.x/passwords/5157" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/pas...</a></p>
</blockquote>
</div></template>


